package com.our.project1.service;

import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.project1.dao.KindClassDao;
import com.our.project1.entity.KindClass;
import com.our.set.service.HzService;

@Service
@Transactional
public class KindClassService {

    @Autowired
    private KindClassDao kindClassDao;
    @Autowired
    private HzService hzService;

    private static Logger logger = LoggerFactory.getLogger(KindClassService.class);

    @Transactional(readOnly = true)
    public Page<KindClass> query(Page<KindClass> page, List<PropertyFilter> filters) {
        Page<KindClass> curPage = kindClassDao.findPage(page, filters);
        return curPage;
    }

    @Transactional(readOnly = true)
    public KindClass queryById(Long id) {
        return kindClassDao.findOneBy("id", id);
    }

    @Transactional(readOnly = true)
    private Long queryCountByNameAndType(String name, Long type) {
        return (Long) kindClassDao.find("select count(a) from KindClass a where a.name = ? and a.type = ?", name, type)
                .get(0);
    }

    public void save(KindClass kindClass) throws Exception {
        try {
            Map<String, String> map = hzService.getMemo(kindClass.getName());
            kindClass.setPy(map.get("py"));
            kindClass.setWb(map.get("wb"));
            if (null != kindClass.getId()) {
                KindClass getKindClass = this.queryById(kindClass.getId());

                if (queryCountByNameAndType(kindClass.getName(), kindClass.getType()).compareTo(1L) > 0) {
                    throw new Exception("在这个类别中，已经存在这个分类值");
                }

                getKindClass.setCode(kindClass.getCode());
                getKindClass.setKind(kindClass.getKind());
                getKindClass.setName(kindClass.getName());
                getKindClass.setPy(kindClass.getPy());
                getKindClass.setWb(kindClass.getWb());
                getKindClass.setType(kindClass.getType());

            } else {

                if (queryCountByNameAndType(kindClass.getName(), kindClass.getType()).compareTo(0L) > 0) {
                    throw new Exception("在这个类别中，已经存在这个分类值");
                }

            }

            kindClassDao.save(kindClass);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }

    public void delete(Long id) throws Exception {
        try {
            kindClassDao.delete(id);
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            throw new Exception(e.getMessage());
        }
    }
}
